# 预编译指令

# rm -rf pre_script && git clone https://gitee.com/getmoon/pre_script.git && ./pre_script/packages_script/pre.sh ${PR_URL}
# rm -rf pre_script && git clone https://gitee.com/getmoon/pre_script.git && ./pre_script/packages_script/pre.sh https://gitee.com/openharmony-sig/flutter_flutter/pulls/29

apt update && apt install -y curl tar unzip python3 tree libc6-dev libc6 binutils wget gcc make gawk bison && apt remove -y nodejs

function get_project_name() {
    # 默认构建项目路径
    DEFALUT_PROJECT='packages/pigeon/example/app'
    local diff_url=$1.diff
    local diff_name=${diff_url##*/}
    local retval=1
    if [ ! -f $diff_name ]
    then
        wget $diff_url
        if [ $? -ne 0 ]
        then
            echo "$DEFALUT_PROJECT"
            reutrn $retval
        fi
    fi
    local diff_str=`cat $diff_name | grep "diff --git"`
    # 匹配a/开头，/ohos/entry/.* 中间的路径，因为不同项目的目录结构不同
    local regex="a/(.*)/ohos/entry/.*"
    for diff in $diff_str
    do
    local tmp=`echo $diff | grep "/"`
    if [ ! -z "$tmp" ]
    local find=0
    then
        if [[ $tmp =~ $regex ]]; then
            tmp=${BASH_REMATCH[1]}
            echo "$tmp"
            retval=0
            find=1
            break
        fi
    fi
    done
    if [ "$find" -eq 0 ];then
        echo "$DEFALUT_PROJECT"
    fi
    return $retval
}

PROJECT=$(get_project_name $1)

# 下载或更新
cloneOrPull() {
    GIT_URL=$1
    TARGET_DIR=$2
    if [ "$TARGET_DIR" == "" ]; then
        var1=${GIT_URL##*/}
        TARGET_DIR=${var1%.*}
    fi
    echo "GIT_URL=$GIT_URL"
    echo "TARGET_DIR=$TARGET_DIR"
    if [ ! -d "$TARGET_DIR/.git" ] || [ "$(git -C $TARGET_DIR status -s)" != "" ]; then
        rm -rf $TARGET_DIR
        mkdir -p $TARGET_DIR
        git clone $GIT_URL $TARGET_DIR
    else
        git -C $TARGET_DIR pull
    fi
}

#!/bin/bash
#拷贝依赖：

#1、flutter仓库
#2、engine产物

cloneOrPull https://gitee.com/getmoon/flutter_env.git
cloneOrPull https://gitee.com/openharmony-sig/flutter_flutter.git
if [ "$(git -C flutter_flutter branch | grep dev)" == "" ]; then
    echo "flutter_flutter create branch dev"
    git -C flutter_flutter checkout -b dev origin/dev
else
    echo "flutter_flutter checkout dev"
    git -C flutter_flutter checkout dev
fi
git -C flutter_flutter pull

#创建npmrc文件
rm -f ~/.npmrc
echo -e "registry=https://repo.huaweicloud.com/repository/npm/\n@ohos:registry=https://repo.harmonyos.com/npm/\nlockfile=false" > ~/.npmrc

rm -rf ~/.ohpm
mkdir ~/.ohpm
touch ~/.ohpm/.ohpmrc
echo -e "registry=https://ohpm.openharmony.cn/ohpm/" > ~/.ohpm/.ohpmrc

#$OHPM_HOME/bin/init

rm -f make.sh
cp pre_script/packages_script/make.sh .

rm -rf hap_out
mkdir hap_out
